package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.IUpvote;
import com.jimikongjian.models.Upvote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17-5-13.
 */
@Service("iUpvoteImpl")
public class IUpvoteImpl implements IUpvote {

    @Autowired
    private IUpvote iUpvote;

    /**
     * 通过id号，获取点赞信息
     *
     * @param id
     * @return List<Upvote>
     */
    @Override
    public List<Upvote> getUpvoteListById(int id) {
        if (id > 0){
            return iUpvote.getUpvoteListById(id);
        }
        return null;
    }

    /**
     * 通过用户id号，获取用户点赞所有文章的信息
     *
     * @param userId
     * @return List<Upvote>
     */
    @Override
    public List<Upvote> getUpvoteListByUserId(int userId) {
        if (userId > 0){
            return iUpvote.getUpvoteListByUserId(userId);
        }
        return null;
    }

    /**
     * 通过文章id号，获取点赞该文章的所有的用户的信息
     *
     * @param articleId
     * @return List<Upvote>
     */
    @Override
    public List<Upvote> getUpvoteListByArticleId(int articleId) {
        if (articleId > 0){
            return iUpvote.getUpvoteListByArticleId(articleId);
        }
        return null;
    }

    /**
     * 通过用户id号，获取用户点赞所有文章的信息
     *
     * @param userId
     * @param articleId
     * @return List<Upvote>
     */
    @Override
    public List<Upvote> getUpvoteListByUserIdAndArticleId(int userId, int articleId) {
        if (userId > 0 && articleId > 0){
            return iUpvote.getUpvoteListByUserIdAndArticleId(userId, articleId);
        }
        return null;
    }

    /**
     * 通过用户id号和文章id号，判断用户是否点赞了该文章
     *
     * @param userId
     * @param articleId
     * @return List<Upvote>
     */
    @Override
    public Boolean isUpvote(int userId, int articleId) {
        List list = getUpvoteListByUserIdAndArticleId(userId, articleId);
        if (list != null && list.size() > 0){
            return true;
        }
        return false;
    }

    /**
     * 新增一个用户点赞
     *
     * @param upvote
     */
    @Override
    public void insertUpvote(Upvote upvote) {
        if (upvote.getUserId() > 0 && upvote.getArticleId() > 0){
            iUpvote.insertUpvote(upvote);
        }
    }

    /**
     * 通过用户的id号，删除该用户的所有的点赞信息， 慎用
     *
     * @param upvote
     */
    @Override
    public void deleteByUserId(Upvote upvote) {
        if (upvote.getUserId() > 0){
            iUpvote.deleteByUserId(upvote);
        }
    }

    /**
     * 通过文章的id号，删除该文章所有的点赞信息，慎用
     *
     * @param upvote
     */
    @Override
    public void deleteByArticleId(Upvote upvote) {
        if (upvote.getArticleId() > 0){
            iUpvote.deleteByArticleId(upvote);
        }
    }

    /**
     * 通过文章的id号和用户的id号，删除该用户点赞的该篇文章的信息
     *
     * @param upvote
     */
    @Override
    public void deleteByUserIdAndArticleId(Upvote upvote) {
        if (upvote.getUserId() > 0 && upvote.getArticleId() > 0){
            iUpvote.deleteByUserIdAndArticleId(upvote);
        }
    }
}
